
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="common/head::head"></head>
<body>

<div th:include="common/header::header"></div>

<div class="layui-container fly-marginTop fly-user-main">

  <div th:replace="common/user-nav"></div>
  
  <div class="fly-panel fly-panel-user" pad20>
      <div class="fly-none" th:if="${user == null}">没有权限,请登录</div>
      <div class="layui-tab layui-tab-brief" lay-filter="user" th:if="${user != null}">
          <ul class="layui-tab-title" id="LAY_mine">
              <li data-type="organization" lay-id="organization" class="layui-this">学校组织管理</li>
          </ul>
          <div class="layui-tab-content" style="padding: 20px 0;">
              <form class="layui-form" th:action="@{/org/add}">
                  <div class="layui-form-item">
                      <label class="layui-form-label">组织名称</label>
                      <div class="layui-input-block">
                          <input type="text" name="name" required  lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
                      </div>
                  </div>
                  <div class="layui-form-item">
                      <div class="layui-input-block">
                          <button class="layui-btn" lay-filter="*" lay-submit>新增</button>
                          <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
                      </div>
                  </div>
              </form>
              <div class="layui-tab-item layui-show">
                  <table class="layui-table" lay-filter="test3" lay-data="{url:'/org/list', id:'test3', page: true}">
                      <thead>
                      <tr>
                          <th lay-data="{field:'id',sort: false, hide:true, width:150}">ID</th>
                          <th lay-data="{field:'gname'}">学校</th>
                          <th lay-data="{field:'pname'}">年级</th>
                          <th lay-data="{field:'name'}">班级</th>
                          <th lay-data="{fixed: 'right', width:185, align:'center', toolbar: '#barDemo'}"></th>
                      </tr>
                      </thead>
                  </table>
                  <script id="barDemo" type="text/html">
                      <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                      <a class="layui-btn layui-btn-xs" lay-event="addSon">添加下级</a>
                  </script>
              </div>
          </div>
      </div>
  </div>
</div>


<div th:include="common/footer::footer"></div>

<script th:src="@{/layui/layui.js}"></script>
<script th:inline="javascript">
  layui.cache.page = 'user';
  layui.cache.user = {
      username: [[${user==null?'游客':user.username}]]
      ,id: [[${user==null?-1:user.id}]]
  };
  layui.config({
    version: "3.0.0"
    ,base: '/mods/'
  }).extend({
    fly: 'index'
  }).use(['fly','table'], function(){
      var table = layui.table
          ,fly = layui.fly;

      //监听行工具事件
      table.on('tool(test3)', function(obj){
          var data = obj.data;
          if(obj.event === 'del'){
              layer.confirm('真的要删除么？', function(index){
                  fly.json("/org/del"
                      ,{id : data.id}
                      ,function (res) {
                          if (res.status === 0) {
                              layer.msg("删除成功");
                              obj.del();
                              layer.close(index);
                          }
                      }
                  );
              });
          } else if(obj.event === 'edit'){
              var g = (data.name==null||data.name=='') && (data.pname==null||data.pname=='');
              var p = (data.name==null||data.name=='') && (data.pname!=null||data.pname!='');
              var v = '';
              if (!g && !p){
                  v = data.name;
              }
              if (p){
                  v = data.pname;
              }
              if (g){
                  v = data.gname;
              }
              layer.prompt({
                  formType: 2
                  ,value: v
              }, function(value, index){
                  fly.json("/org/mod"
                      ,
                      {
                          id : data.id,
                          name :value
                      }
                      ,function (res) {
                          if (res.status === 0) {
                              layer.msg("修改成功");
                              layer.close(index);
                              table.reload('test3', {
                                  where: { //设定异步数据接口的额外参数，任意设
                                  }
                                  ,page: {
                                      // curr: 1 //重新从第 1 页开始
                                  }
                              }); //只重载数据
                          }
                      }
                  );
              });
          } else if(obj.event === 'addSon'){
              var g = (data.name==null||data.name=='') && (data.pname==null||data.pname=='');
              var p = (data.name==null||data.name=='') && (data.pname!=null||data.pname!='');
              if (!g && !p){
                  layer.msg("只支持三级组织");
                  return;
              }
              layer.prompt({
                  title: '输入名称'
                  ,formType: 2
                  ,value: ''
              }, function(value, index){
                  if (value == null || value == ''){
                      layer.close(index);
                      return;
                  }
                  fly.json("/org/add"
                      ,
                      {
                          parentId : data.id,
                          grandId : g?'':data.parentId,
                          name :value
                      }
                      ,function (res) {
                          if (res.status === 0) {
                              layer.close(index);
                              location.href = res.action;
                          }
                      }
                  );
              });
          }
      });
  });

</script>

</body>
</html>